<?php $string = "hi! hi!bye! bye!yo";echo preg_replace("#(.*)([!\?\.\,]+)(?:\r?\n|$)#u", "$2$1", $string);